//
// Created by Administrator on 2021/5/23.
//

/**
 * 自定义协议格式
 */

#ifndef NET_PROTOCOL_H
#define NET_PROTOCOL_H


#include <stdint.h>
#include "buffer.h"


// protocol type
#define PROTOCOL_REGISTER  1
#define PROTOCOL_ACK       2
#define PROTOCOL_PRACK     3
#define PROTOCOL_CANCEL    4
#define PROTOCOL_INVITE    5
#define PROTOCOL_BYE       6
#define PROTOCOL_OPTIONS   7


typedef struct {
    uint32_t type;
    uint32_t id;
    uint32_t cmd;
    uint32_t length;
} __attribute__((packed)) package_head_t;

typedef struct {
    package_head_t head;
    unsigned char data[0];
} __attribute__((packed)) package_t;

package_t *alloc_packet(uint32_t data_len);
void free_package(package_t *package);

int packet_decode(buffer_t *buffer, package_t **package);
int package_encode(buffer_t *buffer, package_t *package);

int server_do_package(package_t *req_package, package_t **resp_package);
int client_do_package(package_t *req_package, package_t **resp_package);

#endif //NET_CLIENT_PROTOCOL_H
